<template>
  <div class="box">
    <div class="querybox">
      <el-input
        v-model="filters.keyword"
        placeholder="请输入查询内容"
        style="width: 200px; right: 10px;"
        @keyup.enter.native="items(1)"
      />
      <el-button
        type="primary"
        @click="items(1)"
      >查询</el-button>
      <el-button
        type="primary"
        @click="add"
      >新增</el-button>
    </div>

    <el-table
      :data="tableData"
      border
    >
      <el-table-column
        prop="title"
        label="标题"
      />
      <el-table-column
        prop="subTitle"
        label="子标题"
      />
      <el-table-column
        label="链接地址"
      >
        <template slot-scope="scope">
          <a :href="scope.row.linkURL" target="_blank" style="text-decoration: underline;">链接地址</a>
        </template>
      </el-table-column>
      <el-table-column
        prop="coverURL"
        label="封面图片"
      >
        <template slot-scope="scope">
          <img
            :src="scope.row.coverURL"
            alt=""
            width="80px"
            height="72px"
          >
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="categories"
        label="类别"
      /> -->
      <el-table-column
        prop="channel"
        label="渠道"
      />
      <el-table-column
        prop="categories"
        label="分类"
      />
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          <div>{{ timeTranslateHMS(scope.row.created) }}</div>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="状态"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-text="启用"
            inactive-text="禁用"
            @change="statusChange(scope.row)"
          />
        </template>
      </el-table-column> -->
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="viewUser(scope.row)"
          >查看</el-button>
          <!-- <el-button type="text" size="small" @click="updUser(scope.row, scope.index)">修改</el-button> -->
          <!-- <el-button
            type="text"
            size="small"
            @click="delUser(scope.row.id)"
          >删除</el-button> -->
          <Del-tip
            type="text"
            size="small"
          >
            <el-button type="primary" size="mini" @click="delUser(scope.row.id)">确定</el-button>
          </Del-tip>
        </template>
      </el-table-column>
    </el-table>
    <el-row style="margin-top: 10px;">
      <el-pagination
        :total="total"
        :current-page="filters.pageNum"
        :page-size="filters.pageSize"
        layout="total, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleSizeChange"
      />
    </el-row>

    <el-dialog
      title="查看"
      :visible.sync="dialogFormVisible"
      :before-close="beforeClose"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        :inline="true"
      >
        <el-form-item
          label="标题"
          prop="title"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.title"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="子标题"
          prop="subTitle"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.subTitle"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="链接地址"
          prop="linkURL"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.linkURL"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="封面图片"
          prop="coverURL"
          :label-width="formLabelWidth"
        >
          <Upload
            v-model="form.coverURL"
            style="width: 185px;"
            :img-url="form.coverURL"
            @on-success="onSuccess"
          />
        </el-form-item>
        <el-form-item
          label="渠道"
          prop="channel"
          :label-width="formLabelWidth"
        >
          <Channel v-model="form.channel" />
        </el-form-item>
        <el-form-item
          label="分类"
          prop="categories"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.categories"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="VIP可见"
          prop="vip"
          :label-width="formLabelWidth"
        >
          <el-switch
            v-model="form.vip"
            style="width: 185px;"
          />
        </el-form-item>
        <el-form-item
          label="排序"
          prop="orderWeight"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.orderWeight"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="创建时间"
          prop="created"
          :label-width="formLabelWidth"
        >
          <el-date-picker
            v-model="form.created"
            type="date"
            placeholder="选择日期"
            disabled
            style="width: 185px;"
          />
        </el-form-item>
      </el-form>
      <!-- <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="姓名" prop="username" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="电话" prop="mobile" :label-width="formLabelWidth">
          <el-input v-model="form.mobile" autocomplete="off" />
        </el-form-item>
      </el-form> -->
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="closeDialogForm('form')"
        >确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import { userinfo } from '@/api/user'
import { items, edit_item, newsDelete } from '@/api/operation'
import { timeTranslateHMS } from '@/utils/formatdate'
import Upload from '@/components/Upload'
export default {
  name: 'Documentation',
  components: {
    Upload
  },
  data() {
    return {
      timeTranslateHMS,
      filters: {
        mobile: '',
        username: '',
        pageNum: 1,
        pageSize: 10,
        keyword: ''
      },
      form: {
        title: '',
        subTitle: '',
        linkURL: '',
        coverURL: '',
        channel: '',
        categories: '',
        vip: '',
        orderWeight: '',
        created: ''
      },
      total: 0,
      rules: {
        title: [
          { required: true, message: '请输入', trigger: 'change' }
        ],
        subTitle: [
          { required: true, message: '请输入', trigger: 'change' }
        ],
        linkURL: [
          { required: true, message: '请输入', trigger: 'change' }
        ],
        coverURL: [
          { required: true, message: '请输入', trigger: 'change' }
        ],
        channel: [
          { required: true, message: '请输入', trigger: 'change' }
        ],
        vip: [
          { required: false, message: '请输入', trigger: 'change' }
        ],
        orderWeight: [
          { required: true, message: '请输入', trigger: 'change' }
        ],
        created: [
          { required: false, message: '请输入', trigger: 'change' }
        ]
      },
      tableData: [],
      formLabelWidth: '120px',
      dialogFormVisible: false,
      query: ''
    }
  },
  mounted() {
    this.items()
  },
  methods: {
    // dialog关闭事件
    beforeClose() {
      this.resetForm('form')
    },
    resetForm(formName) {
      this.form = {}
      this.$refs[formName].resetFields()
      this.dialogFormVisible = false
    },
    // logo上传成功监听
    onSuccess(res) {
      this.form.coverURL = res
      this.$refs.form.validateField('coverURL') // 表单项校验
    },
    add() {
      this.dialogFormVisible = true
    },
    handleSizeChange(pageNum) {
      // 描述：查询结果区域，监听分页
      this.filters.pageNum = pageNum
      this.items()
    },
    // 用户列表
    items(num) {
      // this.filters.pageNum = pageNum || 1
      //   this.filters
      if (num === 1) this.filters.pageNum = 1 // 查询时，分页器数字重置为1
      const pageNum = (num || this.filters.pageNum) - 1
      items({ ...this.filters, pageNum }).then(res => {
        const result = res.data
        this.tableData = result.content
        this.total = result.totalElements
      }).catch(err => {
        console.log(err)
      })
    },
    // 删除用户
    delUser(itemId) {
      // this.tableData.splice(row, 1)
      // this.$message({
      //   message: '删除成功！',
      //   type: 'success'
      // })
      newsDelete({ itemId }).then((res) => {
        this.$message({
          message: '删除成功！',
          type: 'success'
        })
        this.items()
      })
    },
    // 修改用户
    updUser(row, index) {
      this.dialogFormVisible = true
    },
    viewUser(data) {
      this.dialogFormVisible = true
      // this.userinfo(id)
      this.form = { ...data }
      if (!this.form.channel) {
        this.form.channel = '__DEFAULT_CHANNEL__'
      }
      // const { title, subTitle, linkURL, coverURL, channel, created } = data
      // this.form.title =  title,
      // this.form.subTitle = subTitle
      // this.form.linkURL = linkURL
      // this.form.coverURL =  coverURL
      // this.form.channel = channel
      // this.form.created = created
    },
    // 用户列表
    userinfo(id) {
      userinfo({ userId: id }).then(res => {
        // let result = res.data;
        // this.tableData = result.users;
      }).catch(err => {
        console.log(err)
      })
    },
    edit_item() {
      const obj = { ...this.form }
      if (obj.channel === '__DEFAULT_CHANNEL__') {
        obj.channel = ''
      }
      obj.created = timeTranslateHMS(obj.created, null, '/')
      const form = this.$transToFormData(obj)
      edit_item(form).then((res) => {
        this.$message({
          message: '修改成功！',
          type: 'success'
        })
        this.beforeClose()
        this.items()
      })
    },
    // 弹窗关闭触发函数
    closeDialogForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.edit_item()
          //   this.$message({
          //     message: '修改成功！',
          //     type: 'success'
          //   })
        } else {
          return false
        }
      })
    },
    statusChange(row) {
      // {id: row.id, status: row.status}
    }
  }
}
</script>

<style lang="scss" scoped>
.box {
  margin: 20px 30px;
  .form {
    margin-top: 50px;
  }
  .querybox {
    text-align: right;
    margin-bottom: 20px;
  }
}
.box-card {
  margin-bottom: 15px;
}
</style>
